package com.control.qct.businesssteward.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.GoodsSubUnitsAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.SubUnitsModel;
import com.control.qct.businesssteward.utils.EditTextUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 获取最小计量单位
 * Created by Administrator on 2017/12/19 0019.
 */

public class GoodsSubUnitsActivity extends BaseActivity implements HttpUtils.IHttpResult {
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.iv_delete)
    ImageView mIvDelete;
    @BindView(R.id.tv_search)
    TextView mTvSearch;
    @BindView(R.id.rv_goods_measure_unit)
    RecyclerView mRvGoodsMeasureUnit;
    private GoodsSubUnitsAdapter mGoodsSubUnitsAdapter;
    private int mDicSN;
    private List<SubUnitsModel> mSubUnitsModelList;//最小计量单位列表

    public GoodsSubUnitsActivity() {
        super(R.layout.fragment_goods_measure_unit);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mDicSN = extras.getInt("dicSN");
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("最小计量单位");
        mSubUnitsModelList = new ArrayList<>();
        getSubunits("");
        mRvGoodsMeasureUnit.setLayoutManager(new LinearLayoutManager(this));
        mGoodsSubUnitsAdapter = new GoodsSubUnitsAdapter(mSubUnitsModelList);
        mRvGoodsMeasureUnit.setAdapter(mGoodsSubUnitsAdapter);
        mGoodsSubUnitsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mGoodsSubUnitsAdapter.setPosition(position);
                Intent intent = new Intent();
                intent.putExtra("data", ((List<SubUnitsModel>) adapter.getData()).get(position).getTitle());
                intent.putExtra("dicSN", ((List<SubUnitsModel>) adapter.getData()).get(position).getDicSN());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        EditTextUtils.isEditContent(mEtSearch, mIvDelete);
    }


    @OnClick({R.id.iv_delete, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_delete:
                mEtSearch.setText("");
                break;
            case R.id.tv_search:
                getSubunits(mEtSearch.getText().toString());
                break;
        }
    }

    /**
     * 获取最小计量单位
     */
    private void getSubunits(String title) {
        mNetProtocol.getSubunits(this, title, this);
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        List<SubUnitsModel> subUnitsModel = (List<SubUnitsModel>) baseBean.getData();
        mSubUnitsModelList.clear();
        mSubUnitsModelList.addAll(subUnitsModel);
        if (mGoodsSubUnitsAdapter != null) {
            mGoodsSubUnitsAdapter.notifyDataSetChanged();
            for (int i = 0; i < mSubUnitsModelList.size(); i++) {
                if (mSubUnitsModelList.get(i).getDicSN() == mDicSN) {
                    mGoodsSubUnitsAdapter.setPosition(i);
                    mRvGoodsMeasureUnit.smoothScrollToPosition(i);
                }
            }
        }
    }

    @Override
    public void onFail(BaseBean baseBean) {

    }
}
